home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / vol_400 / 424_01 / ed_157 / parse_com.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-02-27  |  3.9 KB  |  145 lines

  1. /*
  2.  * Copyright (C) 1992 by Rush Record
  3.  * Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
  4.  * 
  5.  * This file is part of ED.
  6.  * 
  7.  * ED is free software; you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation.
  9.  * 
  10.  * ED is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
  11.  * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
  12.  * PARTICULAR PURPOSE.  See the GNU General Public License for more details.
  13.  * 
  14.  * You should have received a copy of the GNU General Public License along with ED
  15.  * (see the file COPYING).  If not, write to the Free Software Foundation, 675
  16.  * Mass Ave, Cambridge, MA 02139, USA.
  17.  */
  18. #include "opsys.h"
  19.  
  20. #include <string.h>
  21.  
  22. #include "memory.h"
  23. #include "ctyp_dec.h"
  24. #include "rec.h"
  25. #include "window.h"
  26. #include "ed_dec.h"
  27.  
  28. /******************************************************************************\
  29. |Routine: parse_command
  30. |Callby: command set_param
  31. |Purpose: Parses user input against a list of valid keywords. Returns:
  32. |             -1 if no token was found,
  33. |             -2 if ambiguity existed,
  34. |             -3 if a bad token was found, or
  35. |             n if keyword[n] was matched (zero-based).
  36. |Arguments:
  37. |    prompt is the user prompt, or NULL if we are parsing additional tokens
  38. |            entered on a previous call.
  39. |    buf is the buffer containing the user's input.
  40. |    bufl is the length of buf.
  41. |    length is the (returned) length of the user's buffer.
  42. |    indx is the (returned) current position in the user buffer.
  43. |    commands is the array of valid keywords.
  44. |    num_commands is the number of valid commands.
  45. |    errpart is a string used in reporting of errors.
  46. \******************************************************************************/
  47. Int parse_command(prompt,buf,bufl,length,indx,commands,num_commands,errpart)
  48. Char *prompt,*buf,*commands[],*errpart;
  49. Int bufl,*length,*indx,num_commands;
  50. {
  51.     register Int i,j,k,l,hits;
  52.     Char hit[256];    /* no more than 256 commands or subcommands are allowed */
  53.  
  54.     if(!prompt)    /* if no prompt, we are parsing forward from where we left off */
  55.     {
  56.         l = *length;
  57.         i = *indx;
  58.     }
  59.     else    /* get new response from user */
  60.     {
  61.         if(!(l = inquire(prompt,buf,bufl,1)))
  62.         {
  63.             paint(BOTROW,BOTROW,FIRSTCOL);
  64.             return(-1);
  65.         }
  66.         *length = l;
  67.         i = 0;
  68.         /* Ugly, context specific code for S/ and S / being substitute */
  69.         if(prompt[0] == 'C' && tolower(buf[0]) == 's' && !isalpha(buf[1]) && l >= 5)
  70.         {
  71.             buf[0] = ' ';
  72.             *indx = 0;
  73.             return 27;
  74.         }
  75.     }
  76.     for(;i < l;i++)
  77.         if(!isspace(buf[i]))
  78.         {
  79.             memset(hit,1,num_commands);
  80.             for(j = 0;j < 4;j++)    /* examine four columns in command array */
  81.             {
  82.                 for(k = 0;k < num_commands;k++)
  83.                     if(commands[k][j] != (Char)tolower(buf[i]))
  84.                         hit[k] = 0;
  85.                 for(hits = k = 0;k < num_commands;k++)
  86.                     hits += hit[k];
  87.                 if(!hits)
  88.                     goto bad_command;
  89.                 if(hits == 1)
  90.                 {
  91.                     while((i < l) && !isspace(buf[i]))
  92.                         i++;
  93.                     *indx = i;
  94.                     for(k = 0;k < num_commands;k++)
  95.                         if(hit[k])
  96.                             return(k);
  97.                 }
  98.                 i++;
  99.                 if(!buf[i] || isspace(buf[i]))
  100.                     goto ambiguous_command;
  101.             }
  102.         }
  103.     paint(BOTROW,BOTROW,FIRSTCOL);
  104.     return(-1);    /* no nonblank characters in command, ignore */
  105. ambiguous_command:
  106.     strcpy(buf,"Ambiguous ");
  107.     strcat(buf,errpart);
  108.     strcat(buf,". It matches ");
  109.     for(i = 0;i < num_commands;i++)
  110.         if(hit[i])
  111.         {
  112.             if(--hits > 1)
  113.             {
  114.                 strcat(buf,"'");
  115.                 strcat(buf,commands[i]);
  116.                 strcat(buf,"', ");
  117.             }
  118.             else if(hits == 1)
  119.             {
  120.                 strcat(buf,"'");
  121.                 strcat(buf,commands[i]);
  122.                 strcat(buf,"' ");
  123.             }
  124.             else
  125.             {
  126.                 strcat(buf,"and '");
  127.                 strcat(buf,commands[i]);
  128.                 strcat(buf,"'.");
  129.             }
  130.         }
  131.     slip_message(buf);
  132.     wait_message();
  133.     paint(BOTROW,BOTROW,FIRSTCOL);
  134.     return(-2);
  135. bad_command:
  136.     strcpy(buf,"Unrecognized ");
  137.     strcat(buf,errpart);
  138.     strcat(buf,". Try 'help.'");
  139.     slip_message(buf);
  140.     wait_message();
  141.     paint(BOTROW,BOTROW,FIRSTCOL);
  142.     return(-3);
  143. }
  144.  
  145.